/*
@title: Food Stealer
@description: "Food Stealer" is a simple JavaScript game where players navigate through levels to find stolen apples while avoiding fast food to maintain a healthy diet. Players must also dispose of tempting cakes to progress, adding a layer of challenge. Achieving a high score depends on collecting apples and resisting unhealthy food temptations.
@author: Julia Nadolska
@tags: []
@addedOn: 2024-08-20



RULES
You can move using 'WSAD'
You can reset the level by clicking 'j' or reset the whole game by clicking 'i'.
After the end of the game (if you have been in the room that is full of apples), you can click 'j' to see the end screen.


PLOT
You decided to stay fit and eat clean. Unfortunately, someone has just stolen your apples.
Try to find them, avoiding fast food. Remember to get rid of the cake!
*/

const player = "p"
const apple = "a"
const banana = "b"
const grapes = "g"
const pear = "e"
const lemon = "l"
const watermelon = "w"
const pineapple = "i"
const orange = "o"
const kiwi = "k"
const coconut = "c"
const strawberry = "s"
const wall = "q"
const fries = "f"
const door = "d"
const burger = "r"
const bin = "z"
const cake = "@"
const lava = "!"
const black = "8"

const skrrr = tune`
60.851926977687626: F5/60.851926977687626 + E5/60.851926977687626 + A4-60.851926977687626 + B4-60.851926977687626,
60.851926977687626: C5/60.851926977687626 + E5/60.851926977687626 + F5/60.851926977687626 + D5/60.851926977687626 + A4-60.851926977687626,
60.851926977687626: C5/60.851926977687626 + B4/60.851926977687626 + D5/60.851926977687626 + E5/60.851926977687626 + G4-60.851926977687626,
60.851926977687626: B4/60.851926977687626 + C5/60.851926977687626 + F4-60.851926977687626 + E4-60.851926977687626 + G4-60.851926977687626,
60.851926977687626: B4/60.851926977687626 + A4/60.851926977687626 + G4/60.851926977687626 + F4-60.851926977687626,
60.851926977687626: B4/60.851926977687626 + A4/60.851926977687626 + G4/60.851926977687626,
1582.1501014198782`
const BackgroundSong = tune`
340.90909090909093: A4~340.90909090909093 + C4^340.90909090909093 + D5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + D4-340.90909090909093,
340.90909090909093: A4~340.90909090909093 + D4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + C5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + C5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + C5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + D4-340.90909090909093,
340.90909090909093: A4~340.90909090909093 + D4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + E5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + E5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + E5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + D4-340.90909090909093,
340.90909090909093: A4~340.90909090909093 + D4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + C5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + C5/340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + C5/340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: A4~340.90909090909093 + G4-340.90909090909093,
340.90909090909093: C4^340.90909090909093 + A4~340.90909090909093 + D5/340.90909090909093`
const collecting = tune`
196.07843137254903,
98.03921568627452: C5~98.03921568627452 + B4~98.03921568627452 + A4~98.03921568627452 + G4-98.03921568627452 + F4-98.03921568627452,
98.03921568627452: G4^98.03921568627452 + B4^98.03921568627452 + A4^98.03921568627452 + F4-98.03921568627452,
2745.0980392156866` 
const minus = tune`
103.09278350515464: C4~103.09278350515464 + F4~103.09278350515464 + E4~103.09278350515464 + D4~103.09278350515464 + G4~103.09278350515464,
103.09278350515464: E4~103.09278350515464 + D4~103.09278350515464 + C4~103.09278350515464 + F4~103.09278350515464 + G4~103.09278350515464,
3092.7835051546394`
const trash = tune`
91.1854103343465: E4^91.1854103343465 + D4~91.1854103343465 + F4^91.1854103343465 + G4^91.1854103343465 + A4/91.1854103343465,
91.1854103343465: D4~91.1854103343465 + F4^91.1854103343465 + G4^91.1854103343465 + A4/91.1854103343465 + E4/91.1854103343465,
91.1854103343465: D4~91.1854103343465 + G4^91.1854103343465 + F4^91.1854103343465 + E4^91.1854103343465 + A4/91.1854103343465,
91.1854103343465: G4~91.1854103343465 + F4~91.1854103343465 + E4~91.1854103343465 + D4~91.1854103343465 + A4/91.1854103343465,
2553.191489361702`
const welcome = tune`
98.36065573770492: E4~98.36065573770492 + F4^98.36065573770492 + D4/98.36065573770492,
98.36065573770492: E4~98.36065573770492 + F4^98.36065573770492 + G4-98.36065573770492,
98.36065573770492: F4~98.36065573770492 + G4^98.36065573770492 + A4-98.36065573770492,
2852.4590163934427`

playTune(BackgroundSong, Infinity)

setLegend(
  [ player, bitmap`
.........C......
........C.......
.......6666.....
.......6756.....
.......6666.....
......699996....
......699996....
......699996....
......699996....
.......9999.....
.......9999.....
.......44DD.....
.......44DD.....
.......44DD.....
................
................` ],
  [ apple, bitmap`
................
................
................
.........44.....
.......C444.....
.......C44......
.....333333.....
.....333323.....
.....333233.....
.....333333.....
.....333333.....
.....333333.....
................
................
................
................`],
  [ banana, bitmap`
................
................
...........CC...
............CC..
...........666..
..........6666..
..........6666..
.........6666...
........6666....
......666666....
..666666666.....
..6666666.......
...666..........
................
................
................`],
  [ grapes, bitmap`
................
................
..........H.C...
......H..HCCCH..
.....HCCH.CCC...
......HCCCCHCH..
.........CH.....
.......HCC......
.....H.CCCH.....
....CCCC.C......
....H.C.HCH.....
...HCCCH.H......
..HCC.H.........
....H...........
................
................`],
  [ pear, bitmap`
................
................
................
........C.......
........C.......
.......444......
.......444......
......42444.....
.....4244444....
.....4444444....
.....4444444....
.....4444444....
......44444.....
................
................
................`],
  [ lemon, bitmap`
................
................
........44......
.......444......
.......CC.......
......6666......
.....662666.....
.....626666.....
.....666666.....
.....666666.....
.....666666.....
......6666......
................
................
................
................`],
  [ watermelon, bitmap`
................
................
................
................
.......33.......
......3333......
......3033......
.....333333.....
....33033033....
...4333333334...
...4433333344...
....44444444....
................
................
................
................`],
  [ pineapple, bitmap`
................
........D.......
.....DD.D.DDD...
....D..DDDD.....
.....DDDDDDD....
....D..DDD..D...
.......666......
......F6F66.....
.....66666F6....
.....6F66F66....
.....F66F66F....
.....6F66666....
.....6666F6F....
......6F66F.....
................
................`],
  [ orange, bitmap`
................
................
.........44.....
........444.....
.......44.......
.....999999.....
....99299999....
....92999999....
....99999999....
....99999999....
....99999999....
.....999999.....
................
................
................
................`],
  [ kiwi, bitmap`
................
................
................
.....CCCCCC.....
....CC4044CC....
...CC444044CC...
...C04044444C...
...C44422404C...
...C04422444C...
...C44044404C...
...CC444044CC...
....CC4444CC....
.....CCCCCC.....
................
................
................`],
  [ coconut, bitmap`
................
................
................
.....CCCCCC.....
....CCCCCCCC....
...CCCCCC0CCC...
...CCCCCCCCCC...
...CCCCCC0C0C...
...CCCCCCCCCC...
...CCCCCCCCCC...
...CCCCCCCCCC...
....CCCCCCCC....
.....CCCCCC.....
................
................
................`],
  [ strawberry, bitmap`
................
................
................
................
................
................
.......44.......
......4444......
.....333333.....
.....363363.....
.....333633.....
.....333333.....
......3633......
.......33.......
................
................`],
  [ wall, bitmap`
1111111111111111
1LLLLLLLLLLLLLL1
1L111111111111L1
1L111111111111L1
1L111111111111L1
1L111LLLLLL111L1
1L111LLLLLL111L1
1L111LLLLLL111L1
1L111LLLLLL111L1
1L111LLLLLL111L1
1L111LLLLLL111L1
1L111111111111L1
1L111111111111L1
1L111111111111L1
1LLLLLLLLLLLLLL1
1111111111111111`],
  [ fries, bitmap`
................
................
................
.......6.6......
......6666.6....
.....6666666....
......66666.....
......33333.....
......33333.....
......33333.....
......33333.....
......33333.....
................
................
................
................`],
  [ door, bitmap`
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CC666CCCCCCCCC1
1CCCC6CCCCCCCCC1
1CCCC6CCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1
1CCCCCCCCCCCCCC1`],
  [ burger, bitmap`
................
................
................
................
................
................
.....9999999....
.....9224429....
.....3234323....
.....2266626....
.....9CCCCC9....
.....9999999....
................
................
................
................`],
  [ bin, bitmap`
DDD.............
DD4D............
4DDD............
DDDD............
D4DD.8005307....
DDDD6D0DDD077...
.DDDD004DD0D7...
....D00DDD0D7...
....DD0DD4DD7...
....DDDD4DDD....
....D4DDDDD4....
....4DDDDD4D....
....DD4D4DDD....
....DDDDDDDD....
.....DDDDDD.....
................`],
  [ cake, bitmap`
................
................
................
................
................
................
.....232222.....
....22222322....
...2232222232...
...CC22CCCC2C...
...CC2CCCCCCC...
....CCCCCCCC....
................
................
................
................`],
  [ lava, bitmap`
9999999999999999
9666666666666669
9699999999999969
9699999999999969
9699666666669969
9699699999969969
9699699999969969
9699699999969969
9699699999969969
9699699999969969
9699699999969969
9699666666669969
9699999999999969
9699999999999969
9666666666666669
9999999999999999`],
  [ black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  
  
  
  
  
)

setSolids([wall, player, cake, door, lava])
var score  = 0
addText(`Score: ${score}`,{x: 10, y: 0, color: color`5`})

let level = 0
const levels = [
  map`
p.q.....
@.q.qq..
afq.fq..
.....q..
z..q.qdq
qq.q.q..
a..q.q.a
...q...f`,
  map`
z.......
.qfq..q.
...q....
..aq..f.
.qdq.aqq
.a...@p.
..qqq...
..f.....`,
  map`
....a....
.q....q..
.fzq..qq.
.qqf...q.
.q.@...q.
.qpqq.a..
.qdq...q.
...qf.q..
aqqq.....`,
  map`
q..qqqqqqaf
q..........
q.qqqqqqq..
q.q.....q.q
q.q.q.q.q.q
q.q.q.q.q.q
q.q.qaq.q.q
q.qpq.qaq.q
..qqq.qdq.q
a..zq.@....
qqqqqqqq..f`,
  map`
a...q......
..q.qqq.qdq
..q...q..q.
qqq..fqq...
z.qq.a...q.
.f.qqqq.qa.
.q.qqf...q.
....q.....q
.q.....p@.f
...aqqqq.q.
........qq.`,
  map`
qqqqqqqqqqqqq
q.....a.....p
q.qqqqqqqqqzq
q.....a...q.q
q.qfqqqqq.q.q
q.q.......q.q
q.qfqqdqq.q.q
q.q....a..q.q
q.q.qqqqq.q.q
q.q...af..q.q
q.qqqqqqqqq@q
q.a.........q
qqqqqqqqqqqqq`,
  map`
p...........z
..qqqqqqqqqf.
qqq......aq..
..q.......q..
..q.......q..
.fq.qqqqqqq..
.............
..qqqqqqqqq..
q.qaf....aqq.
q.q.......q.a
q.q.....f.q..
.@qqdqqqq.qf.
.............`,
  map`
pq......zq...
.q.q.q.q.q.q.
.q.f.q.q.q.q.
.q.q.q.q.q.q.
.qaq.q.q...qa
.q.q.f.q.qqdq
...q.q.q.q...
.q.q.q.q.q...
.q.q.q.q.q.q.
.@.....q..aq.
.q.q.q.q.q.q.
.q.q.qaq.q...
...q...q.....`,
  map`
.a...........
.....f...a...
.............
..qqqq.qqqq.f
..q..q.q..q..
..qqqqzqqqq.p
..f........qq
......q......
..q...q...q..
..q.a.....q..
..q.....f.q..
...qqqdqqq.@.
.a..........f`,
  map`
.................
a!!!.qqqqqqq.!!!a
.!!.....z@....!!.
.!...qqqqqqq...!.
....qqqqqqqqq....
.q..qq!!q!!qq..q.
.q.qq!!!q!!!qq.q.
.q.qq!!qqq!!qq.q.
.qaqqqqqqqqqqqaq.
.q..qqqq!qqqq..q.
.q..qqqqqqqqq..q.
.q....q!q!q....q.
......q!!!q......
.!......a......!.
.!!.....p.....!!.
.!!!.qqqdqqq.!!!.
a...............a`,
  map`
8`,
  map`
!!!!!!!!!
!aaaaaaa!
!aaaaaaa!
!aaaaaaa!
!aaaaaaa!
!aaaaaaa!
!aaapaaa!
!!!!!!!!!`,
  map`
888
888
888`,
]
/*const grass = [bitmap`
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444`] */
let levelNumber = 0
setMap(levels[level])

/*setBackground(grass) */

setPushables({
  [ player ]: [cake]
})

onInput("s", () => {
  getFirst(player).y += 1
})
onInput("w", () => {
  getFirst(player).y -= 1
})
onInput("d", () => {
  getFirst(player).x += 1
})
onInput("a", () => {
  getFirst(player).x -= 1
})

onInput("j", () => {
  setMap(levels[levelNumber])
})
onInput("i", () => {
  setMap(levels[level])
  
})

afterInput(() => {
  let character = getFirst(player)
  let apples = getAll(apple)
  let friess = getAll(fries)
  let cakee = getFirst(cake)
  let bino = getFirst(bin)
  let dooro = getFirst(door)
  let counter = 0
  
  if(tilesWith(bin, cake).length >= 1){
    cakee.remove()
    bino.remove()
    playTune(trash,1)
    counter = 1
    addText(`Door open!`,{x: 5, y: 6, color: color`H`})
  }
  if(counter == 1){
    setSolids([wall, player, cake, lava])
  }
  if(tilesWith(player, door).length >= 1){
      playTune(welcome,1)
      levelNumber++
      setMap(levels[levelNumber])
      counter = 0
      setSolids([wall, player, cake, door, lava])
  }
  if(tilesWith(player, lava).length >= 1){
      playTune(skrrr, 1)
      setMap(levels[level])
  }
  if(levelNumber == 9){
    addText(`FINAL!!!`,{x: 7, y: 13, color: color`3`})
    
  }
  if(levelNumber == 10 && score >= 38){
      setMap(levels[levelNumber+1])
      addText(`Thanks for \n playing!`,{x: 5, y: 6, color: color`0`})
      if(score >= 100){
        addText(`Thanks for \n playing!`,{x: 5, y: 6, color: color`2`})
        addText(`You scored ${score}`,{x: 4, y: 8, color: color`2`})
        setMap(levels[levelNumber])
        
      }
  }else if(levelNumber == 10){
    clearText()
    addText(`Thanks for \n playing!`,{x: 5, y: 6, color: color`2`})
    addText(`You scored ${score}`,{x: 4, y: 8, color: color`2`})
     
  }
      
  
  
  for(let i = 0; i < apples.length; i++){
    if(character.x === apples[i].x && character.y === apples[i].y){
      apples[i].remove()
      playTune(collecting, 1)
      score += 1;
      clearText()
      addText(`Score: ${score}`,{x: 10, y: 0, color: color`5`})
    
    }
  }

  for(let i = 0; i <friess.length; i++){
    if(character.x === friess[i].x && character.y === friess[i].y){
      friess[i].remove()
      playTune(minus, 1)
      score -=1
      clearText()
      addText(`Score: ${score}`,{x: 10, y: 0, color: color`5`})
    }
  }
  
});
